package ch11
{
	import flash.display.Sprite;
	import flash.events.Event;

	import utils.Ball;

	/**
	 * 两个轴上的动量守恒
	 * @author sjp
	 *
	 */
	public class Billiard3 extends Sprite
	{
		private var ball0:Ball;
		private var ball1:Ball;
		private var bounce:Number = -1.0;

		public function Billiard3()
		{
			init();
		}

		private function init():void
		{
			ball0 = new Ball(100);
			ball0.mass = 2;
			ball0.x = stage.stageWidth - 200;
			ball0.y = stage.stageHeight - 200;
			ball0.vx = Math.random() * 10 - 5;
			ball0.vy = Math.random() * 10 - 5;
			addChild(ball0);
			ball1 = new Ball(60);
			ball1.mass = 1;
			ball1.x = 100;
			ball1.y = 100;
			ball1.vx = Math.random() * 10 - 5;
			ball1.vy = Math.random() * 10 - 5;
			addChild(ball1);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void
		{
			ball0.x += ball0.vx;
			ball0.y += ball0.vy;
			ball1.x += ball1.vx;
			ball1.y += ball1.vy;
			checkCollision(ball0, ball1);
			checkWalls(ball0);
			checkWalls(ball1);
		}

		private function checkWalls(ball:Ball):void
		{
			if (ball.x + ball.radius > stage.stageWidth)
			{
				ball.x = stage.stageWidth - ball.radius;
				ball.vx *= bounce;
			}
			else if (ball.x - ball.radius < 0)
			{
				ball.x = ball.radius;
				ball.vx *= bounce;
			}
			if (ball.y + ball.radius > stage.stageHeight)
			{
				ball.y = stage.stageHeight - ball.radius;
				ball.vy *= bounce;
			}
			else if (ball.y - ball.radius < 0)
			{
				ball.y = ball.radius;
				ball.vy *= bounce;
			}
		}

		private function checkCollision(ball0:Ball, ball1:Ball):void
		{
			var dx:Number = ball1.x - ball0.x;
			var dy:Number = ball1.y - ball0.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (dist < ball0.radius + ball1.radius)
			{
				// 计算角度和正余弦值 
				var angle:Number = Math.atan2(dy, dx);
				var sin:Number = Math.sin(angle);
				var cos:Number = Math.cos(angle);
				/**
				 * 旋转坐标至x轴水平，使得碰撞只与x轴有关，与y轴无关。
				 * 记住 vy 的值永远不变。
				 * */
				//因为我们使用 ball0 作为“中心点”，它的坐标就是 0,0。这个值在旋转后都不会改变，只要写：
				// 旋转 ball0 的位置 
				var x0:Number = 0;
				var y0:Number = 0;
				// 旋转 ball1 的位置
				var x1:Number = dx * cos + dy * sin;
				var y1:Number = dy * cos - dx * sin;
				// 旋转 ball0 的速度 
				var vx0:Number = ball0.vx * cos + ball0.vy * sin;
				var vy0:Number = ball0.vy * cos - ball0.vx * sin;
				// 旋转 ball1 的速度
				var vx1:Number = ball1.vx * cos + ball1.vy * sin;
				var vy1:Number = ball1.vy * cos - ball1.vx * sin;
				// 碰撞的作用力 
				/**代码优化*/
				var vxTotal:Number = vx0 - vx1;
				vx0 = ((ball0.mass - ball1.mass) * vx0 + 2 * ball1.mass * vx1) / (ball0.mass + ball1.mass);
				vx1 = vxTotal + vx0;
				x0 += vx0;
				x1 += vx1;

				// 将位置旋转回来 
				var x0Final:Number = x0 * cos - y0 * sin;
				var y0Final:Number = y0 * cos + x0 * sin;
				var x1Final:Number = x1 * cos - y1 * sin;
				var y1Final:Number = y1 * cos + x1 * sin;

				/**
				 * 最终的位置与系统中心点 ball0 的位置相对的。
				 * 因此，需要把它们都加到 ball0 的位置上，从而得到实际在屏幕上的位置。
				 * */
				// 将位置调整为屏幕的实际位置
				ball1.x = ball0.x + x1Final;
				ball1.y = ball0.y + y1Final;
				ball0.x = ball0.x + x0Final;
				ball0.y = ball0.y + y0Final;
				// 将速度旋转回来 
				ball0.vx = vx0 * cos - vy0 * sin;
				ball0.vy = vy0 * cos + vx0 * sin;
				ball1.vx = vx1 * cos - vy1 * sin;
				ball1.vy = vy1 * cos + vx1 * sin;
			}
		}
	}
}